﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BLL;
using System.Text.RegularExpressions;

namespace CorporateGymManagementSystem
{
    /// <summary>
    /// Interaction logic for GymEmployeeCreationWindow.xaml
    /// </summary>
    public partial class GymEmployeeCreationWindow : Window
    {
        public GymEmployeeCreationWindow()
        {
            InitializeComponent();
        }

        private void addGymEmplyeeButton_Click(object sender, RoutedEventArgs e)
        {
            if (validateForm() == true)
            {
                GymWorker worker = new GymWorker
                {
                    FirstName = firstName.Text,
                    LastName = lastName.Text,
                    Phone = phone.Text,
                    Email = email.Text,
                    UserName = username.Text,
                    Password = password.Password,
                    IsAdminitrator = adminRadioButton.IsChecked.Value
                };

                if (GymWorkerManager.Instance.AddWorker(worker) > -1)
                {
                    result.Content = "worker Has Added";
                    this.Close();
                }
            }
        }

        private bool validateForm()
        {
            bool isValid = true;

            firstName.Background = Brushes.White;
            lastName.Background = Brushes.White;
            email.Background = Brushes.White;
            phone.Background = Brushes.White;
            username.Background = Brushes.White;
            password.Background = Brushes.White;
            firstNameError.Content = string.Empty;
            lastNameError.Content = string.Empty;
            emailError.Content = string.Empty;
            phoneError.Content = string.Empty;
            userNameError.Content = string.Empty;
            passwordError.Content = string.Empty;

            if (firstName.Text.Equals(""))
            {
                isValid = false;
                firstName.Background = Brushes.Pink;
                firstNameError.Content = "Please Fill First Name";
            }

            if (lastName.Text.Equals(""))
            {
                isValid = false;
                lastName.Background = Brushes.Pink;
                lastNameError.Content = "Please Fill Last Name";
            }

            if (email.Text.Equals("") || EmailService.IsValidEmail(email.Text) == false)
            {
                isValid = false;
                email.Background = Brushes.Pink;
                emailError.Content = "Please Fill Valid Email";
            }

            if (phone.Text.Equals(""))
            {
                isValid = false;
                phone.Background = Brushes.Pink;
                phoneError.Content = "Please Fill Phone Number";
            }

            if (username.Text.Equals(""))
            {
                isValid = false;
                username.Background = Brushes.Pink;
                userNameError.Content = "Please Fill User Name";
            }
            else if (GymWorkerManager.Instance.IsUserNameValid(username.Text) == false)
            {
                isValid = false;
                username.Background = Brushes.Pink;
                userNameError.Content = "The User Name Already Taken";
            }

            if (password.Password.Equals(""))
            {
                isValid = false;
                password.Background = Brushes.Pink;
                passwordError.Content = "Please Fill Password";
            }

            return isValid;
        }
    }
}
